package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;


import model.Contato;

public class ContatoDAO {
    
    private Connection con;

    public ContatoDAO() throws ClassNotFoundException {
        this.con = new ConnectionFactory().getConnection();
    }
    
    public void adiciona(Contato contato)
    {
        String insert = "insert into contatos ( nome, email, endereco ) values (  ?, ?, ? )";
        try {
              PreparedStatement stmt = con.prepareStatement(insert);
              
              stmt.setString( 1, contato.getNome( ) );
              stmt.setString( 2, contato.getEmail( ) );
              stmt.setString( 3, contato.getEndereco() );
              
              stmt.execute();
              stmt.close();
            }
        catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    
    public void alterar(Contato contato)
    {
        String alterar = "update contatos set nome= ?, email= ?, endereco= ? where id = ?";
        try {
              PreparedStatement stmt = con.prepareStatement(alterar);
              stmt.setString( 1, contato.getNome( )     );
              stmt.setString( 2, contato.getEmail( )    );
              stmt.setString( 3, contato.getEndereco() );
              stmt.setInt   ( 4, contato.getId( )       );
              stmt.execute();
              stmt.close();
            }
        catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    
    public void delete(int id)
    {
        String del = "delete from contatos where id  = ? ";
        try {
              PreparedStatement stmt = con.prepareStatement(del);
              stmt.setInt( 1, id );
              stmt.execute();
              stmt.close();
            }
        catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    
    
	public List<Contato> listar( ) throws SQLException
	{
		
		String	sql = "select * from contatos";
		
		try {
				
			List<Contato> lista_contatos = new ArrayList<Contato>();
			PreparedStatement stmt = this.con.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();

			while (rs.next()) {
				Contato contato = new Contato( );
				contato.setId      ( rs.getInt   ( "id"       ) );
				contato.setNome    ( rs.getString( "nome"     ) );
				contato.setEmail   ( rs.getString( "email"    ) );
				contato.setEndereco( rs.getString( "endereco" ) );
				lista_contatos.add(contato);

			}
			
			rs.close();
			stmt.close();
			
			return lista_contatos;
			
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	
	
	public Contato selecionar( int id )
	{
		try{
			Contato contato = new Contato( );
			PreparedStatement stmt = this.con.prepareStatement("select * from contatos where id = ?");
			stmt.setInt( 1, id );
			ResultSet rs = stmt.executeQuery();
			rs.next();
			contato.setId      ( rs.getInt   ( "id"       ) );
			contato.setNome    ( rs.getString( "nome"     ) );
			contato.setEmail   ( rs.getString( "email"    ) );
			contato.setEndereco( rs.getString( "endereco" ) );
			return contato;
			}catch (Exception e) {
			throw new RuntimeException(e);
    	  }
	}
	
}
